package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hee.erp.filter.LoginContextHolder;
import com.hee.erp.modules.entity.SysUser;
import com.hee.erp.modules.entity.SysUserGrowLog;
import com.hee.erp.modules.mapper.SysUserGrowLogMapper;
import com.hee.erp.modules.service.ISysUserGrowLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.modules.service.ISysUserService;
import com.hee.erp.pojo.LoginInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-04-25
 */
@Service
@Transactional
public class SysUserGrowLogServiceImpl extends ServiceImpl<SysUserGrowLogMapper, SysUserGrowLog> implements ISysUserGrowLogService {

    @Autowired
    ISysUserService sysUserService;

    @Override
    public List<SysUserGrowLog> getListByUserId(String userId) {

        LambdaQueryWrapper<SysUserGrowLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserGrowLog::getUserId,userId);
        List<SysUserGrowLog> list = this.list(queryWrapper);
        setForeignKeyValue(list);

        if(list == null || list.size() == 0) {
            LoginInfo loginInfo = LoginContextHolder.get();
            SysUserGrowLog sysUserGrowLog = new SysUserGrowLog();
            sysUserGrowLog.setUserId(userId);
            sysUserGrowLog.setCreateUser(loginInfo.getUserId());
            SysUser currentUser = sysUserService.getById(loginInfo.getUserId());
            sysUserGrowLog.setCreateUserName(currentUser.getUserName());
            sysUserGrowLog.setGrowDate(LocalDate.now());

            list.add(sysUserGrowLog);
        }
        return list;
    }

    @Override
    public void saveUserGrowLogList(List<SysUserGrowLog> list) {
        LoginInfo loginInfo = LoginContextHolder.get();
        list.forEach(item -> {
            if (StringUtils.isBlank(item.getId()) || item.getId().contains("-")) {
                item.setId(null);
                item.setCreateUser(loginInfo.getUserId());
                item.setCreateTime(LocalDateTime.now());
                item.setModifyUser(loginInfo.getUserId());
                item.setModifyTime(LocalDateTime.now());
            }else {
                item.setModifyUser(loginInfo.getUserId());
                item.setModifyTime(LocalDateTime.now());
            }
        });

        this.saveOrUpdateBatch(list);
    }

    private void setForeignKeyValue(List<SysUserGrowLog> list) {
        //设置createUserName
        list.forEach(item -> {
            //根据createUser获取sysUser
            SysUser createUser = sysUserService.getById(item.getCreateUser());
            item.setCreateUserName(createUser.getUserName());
        });
    }
}
